package com.taptech.doufu.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.util.HttpRequestUtil;
import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.ShareBeansInfo;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.view.SharePicPopupWindow;
import com.taptech.doufu.ui.view.ShareTopPopupWindow;

public class AnswerStarActivity extends DiaobaoBaseActivity {
	WebView webView;
	TextView title;
	private String url = Constant.HOST_NAME_SECOND
			+ "/index.php/question/question_test";
	private ImageView share;
	private LinearLayout mWaitProgressBarLayout;
	private String shareDes,shareUrl, shareTitle,imgUrl;
	private boolean initLogin=false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_answer_star);
		webView = (WebView) findViewById(R.id.activity_answer_start_wb);
		title = (TextView) findViewById(R.id.activity_answer_start_title);
		title.setText("腐化度测试");
		share = (ImageView) findViewById(R.id.activity_answer_start_share);
		mWaitProgressBarLayout = (LinearLayout) findViewById(R.id.answer_wait_progressbar_layout);
		mWaitProgressBarLayout.setVisibility(View.VISIBLE);
		webView.getSettings().setJavaScriptEnabled(true);
		// HttpRequestUtil.getCookie(this, url);
		webView.addJavascriptInterface(new JavascriptInterface(this),
				"imagelistner");
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(
					android.webkit.WebView view, String url) {
				// view.loadUrl(url);
				return false;
			};

			@Override
			public void onPageFinished(WebView view, String url) {
				mWaitProgressBarLayout.setVisibility(View.GONE);
			}
		});
		// HttpRequestUtil.getCookie();
		TTLog.s("xxxx========"+url);
		HttpRequestUtil.getCookie(this, url);
		webView.loadUrl(url);
		if(AccountService.getInstance().isLogin()){
			initLogin=true;
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (AccountService.getInstance().isLogin()&&initLogin==false) {
			HttpRequestUtil.getCookie(this, url);
			webView.loadUrl(url);
			initLogin=true;
		}
	}

	public void backOnclick(View view) {
		finish();
	}

	// js通信接口
	public class JavascriptInterface {

		private Context context;

		public JavascriptInterface(Context context) {
			this.context = context;
		}

		public void changeTitle(String titleName) {
			TTLog.s("xxxx========" + titleName);
			Message msg = new Message();
			msg.what = 1;
			msg.obj = titleName;
			mHandler.sendMessage(msg);
		}

		public void login() {
			AccountService.getInstance().jumpToLogin();
		}

		/** type=1 ：分享我们，2：成绩，3：排行榜 */
		public void share(String type, String des, String url) {
			shareDes = des;
			shareUrl = url;
			Message msg = new Message();
			msg.what = 2;
			msg.obj = type;
			mHandler.sendMessage(msg);
		}
		
		public void richShare(String des,String url, String title, String img, String callback){
			shareTitle = title;
			shareDes=des;
			shareUrl=url;
			imgUrl= img;
//			shareCallBack = callback;
			Message msg=new Message();
			msg.what=2;
			msg.obj=2;
			mHandler.sendMessage(msg);
		}
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				if (msg.obj != null) {
					title.setText(msg.obj.toString());
				}
				break;
			case 2:
				if (msg.obj != null) {
					if ("1".equals(msg.obj.toString())) {
						SharePicPopupWindow popupWindow = new SharePicPopupWindow(
								AnswerStarActivity.this);
						ShareBeansInfo shareInfo = new ShareBeansInfo(
								ShareBeansInfo.OWN_SHARE_FLAG);
						popupWindow.setShareBeans(shareInfo);
						// 显示窗口
						popupWindow.showAtLocation(share, Gravity.BOTTOM
								| Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
					} else if ("2".equals(msg.obj.toString())) {
						SharePicPopupWindow popupWindow=new SharePicPopupWindow(AnswerStarActivity.this);
						ShareBeansInfo shareInfo=new ShareBeansInfo(ShareBeansInfo.ANSWER_SORCE_SHARE_FLAG);
						shareInfo.setShartText(shareTitle);
						shareInfo.setTitle(shareTitle);
						shareInfo.setDescription(shareDes);
						shareInfo.setShareUrl(shareUrl);
						shareInfo.setImagUrl(imgUrl);
						popupWindow.setShareBeans(shareInfo);
						//显示窗口
						popupWindow.showAtLocation(share, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
					} else if ("3".equals(msg.obj.toString())) {
						share.setVisibility(View.VISIBLE);

					}

				}
				break;
			default:
				break;
			}

		};
	};

	public void shareOnlick(View view) {
		// TODO Auto-generated method stub
		TTLog.s(shareDes + "===shareDes===shareUrl===" + shareUrl);

		ShareTopPopupWindow popupWindow = new ShareTopPopupWindow(
				AnswerStarActivity.this);
		ShareBeansInfo shareInfo = new ShareBeansInfo(shareDes, shareUrl,
				ShareBeansInfo.RANK_SORCE_SHARE_FLAG);
		popupWindow.setShareBeans(shareInfo);
		// 显示窗口
		popupWindow.showAsDropDown(view, 0, 0); // 设置layout在PopupWindow中显示的位置
	}
}
